import axios from 'axios'
import router from '@/router'

// create an axios instance
const service = axios.create({
  method: 'GET'
})

// request interceptor
service.interceptors.request.use(
  config => {
    config.headers.Authorization = `${localStorage.getItem('token')}`;
    return config
  },

  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    console.log('err' + error) // for debug
    switch (error.response.status) {
      case 401:
        router.push('/login');
        break;
    
      default:
        break;
    }
    return error
  }
)

export default service
